//
//  SceneRecorder.h
//  Moo-O Touch
//
//  Created by Reisen on 11/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

//
//ScreenCaptureView.h
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

/**
 * Delegate protocol.  Implement this if you want to receive a notification when the
 * view completes a recording.
 *
 * When a recording is completed, the ScreenCaptureView will notify the delegate, passing
 * it the path to the created recording file if the recording was successful, or a value
 * of nil if the recording failed/could not be saved.
 */
@protocol VideoRecorderDelegate <NSObject>
- (void) recordingFinished:(NSString*)outputPathOrNil;
@end

/**
 * ScreenCaptureView, a UIView subclass that periodically samples its current display
 * and stores it as a UIImage available through the 'currentScreen' property.  The
 * sample/update rate can be configured (within reason) by setting the 'frameRate'
 * property.
 *
 * This class can also be used to record real-time video of its subviews, using the
 * 'startRecording' and 'stopRecording' methods.  A new recording will overwrite any
 * previously made recording file, so if you want to create multiple recordings per
 * session (or across multiple sessions) then it is your responsibility to copy/back-up
 * the recording output file after each session.
 *
 * To use this class, you must link against the following frameworks:
 *
 *  - AssetsLibrary
 *  - AVFoundation
 *  - CoreGraphics
 *  - CoreMedia
 *  - CoreVideo
 *  - QuartzCore
 *
 */

@interface VideoRecorder : NSObject {
    //video writing
    AVAssetWriter *videoWriter;
    AVAssetWriterInput *videoWriterInput;
    AVAssetWriterInputPixelBufferAdaptor *avAdaptor;
    
    
    //Audio Writing.
    AVAssetWriterInput* audioWriterInput;
    
    //recording state
    BOOL _recording;
    void* bitmapData;
    
    CGRect frame;
    
    NSString* recordPath;
    
    NSDate* startedAt;
    @public
    
    double startTime;
}

- (id) initWithWidth:(int)w Height:(int)h;

//for recording video
- (bool) startRecording;
- (void) stopRecording;
- (void) updateStartTime;
- (void) encodeFrameAtTime: (CMTime) timestamp;
- (BOOL) encodeAudioFrame:(CMSampleBufferRef)buffer;
- (BOOL) encodeVideoFrame:(CMSampleBufferRef)buffer;

- (CGContextRef) createBitmapContextOfSize:(CGSize) size;
- (void) setFrameFromUIView:(UIView*)view withFrame:(CGRect) videoFrameLOL;

@property (nonatomic, strong) NSString* recordPath;

//for accessing the current screen and adjusting the capture rate, etc.
@property(strong) UIImage* currentScreen;
@property(assign) int bitRate;
@property(nonatomic, weak) id<VideoRecorderDelegate> delegate;


@property(strong) UIImage* foreground;
@end